package com.ctrip.demo.leetcode;

import java.util.HashMap;
import java.util.Map;

public class Jump {

    public static void main(String[] args) {
        int[] nums = {2, 2, 0, 0, 2};
        System.out.println(jump1(nums));

        System.out.println("=====================================");

        System.out.println(jump(nums));
    }

    public static int jump(int[] nums) {
        int length = nums.length;

        if (length == 0) {
            return 0;
        }

        int min = (length - 1) % nums[0] == 0 ? (length - 1) / nums[0] : (length - 1) / nums[0] + 1 ;
        for (int i = 1; i < nums.length; i++) {
            int temp = 0;
            if ((length - (i+1)) % nums[i] == 0) {
                temp = (length - (i+1)) / nums[i];
            } else {
                temp = (length - (i+1)) % nums[i] + 1;
            }

            if (min >= temp) {
                min = temp;
            }
        }

        return min;
    }


    public static int jump1(int[] nums) {
        if (nums.length == 1) {
            return 0;
        }
        int reach = 0;
        int nextreach = nums[0];
        int step = 0;
        for (int i = 0; i < nums.length; i++) {
            nextreach = Math.max(i + nums[i], nextreach);
            if (nextreach >= nums.length - 1) {
                return (step + 1);
            }
            if (i == reach) {
                step++;
                reach = nextreach;
            }
        }
        return step;
    }
}
